# 主程序入口
from datetime import datetime

from src.spark_edu_rag.cache import redis_client
from src.spark_edu_rag.db import mysql_client
from src.spark_edu_rag.retrieval import bm25_search
from src.spark_edu_rag.utils import get_logger


class MysqlSystem:
    def __init__(self):
        self.logger = get_logger(__name__)
        self.mysql_client = mysql_client
        self.redis_client = redis_client
        self.bm25_search = bm25_search


    def query(self,query):
        """根据输入查询问题，检索并返回相关问题"""
        start_time = datetime.now()
        results = self.bm25_search.query(query)
        end_time = datetime.now()
        if results:
            self.logger.info(f"查询问题：{query}，答案：{results}，耗时：{end_time - start_time}")
            return results
        else:
            self.logger.info(f"查询问题：{query}，未找到相关问题")
            return []

def main():
    mysql_system = MysqlSystem()
    print("欢迎使用Spark-Edu-RAG系统")
    try:
        while True:
            query = input("\n请输入查询问题（输入exit退出）：")
            if query.lower() == 'exit':
                break
            results = mysql_system.query(query)
            print(results)
    except Exception as e:
        mysql_system.logger.error(f"查询问题时发生异常：{e}")
        raise ValueError(f"查询问题时发生异常：{e}")

if __name__ == '__main__':
    main()
